package com.supreme.studyserver.component;

import com.external.study.service.ImportFileService;
import com.supreme.commontools.FilePathUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.time.LocalDateTime;

@Component
public class QumBeanFactory implements BeanFactoryAware {

    public static DefaultListableBeanFactory beanFactory;

    private final Path beanName = FilePathUtils.getPath("/temp-data/bean-name.log");

    public QumBeanFactory() {
        FilePathUtils.createFile(beanName);
        FilePathUtils.writeFile(beanName, String.valueOf(LocalDateTime.now()));
    }

    @Override
    public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException {
        QumBeanFactory.beanFactory = (DefaultListableBeanFactory) beanFactory;

        for (String beanDefinitionName : QumBeanFactory.beanFactory.getBeanDefinitionNames()) {
            System.out.println("beanDefinitionName = " + beanDefinitionName);
            FilePathUtils.appendWriteFile(beanName, beanDefinitionName);
        }

        ImportFileService importFileService = beanFactory.getBean(ImportFileService.class);
        System.out.println("QumBeanFactory => ImportFileService: " + importFileService);
    }
}
